#!/bin/bash

hostname=`hostname`
gateway_ip="192.168.20.1"
domain=foo

function get_ip {
	echo `ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
}

function update_ns {
	ip=`get_ip`
	# add default route to host and update ns
	cat <<END > /tmp/nsupdate.txt
server $gateway_ip
update delete ${hostname}.$domain
update add ${hostname}.$domain  86400 A $ip
send
END

	route add default gw $gateway_ip
	nsupdate /tmp/nsupdate.txt
}

if [[ -n `get_ip` ]] ; then # if manually set ip address
	# use host dns server
	rm /etc/resolv.conf
	cat <<END > /etc/resolv.conf
nameserver $gateway_ip
domain $domain
search $domain
END
else # if ip not set, use dhcp
	/sbin/dhclient eth0
fi

update_ns
exit 0
